package
{
	import flash.display.MovieClip;

	public class ExitObject
	{
		public var name:String;
		public var pos:Vector2;
		public var trigger:Object;
		public var isOpen:Boolean;
		public var r:Number;
		public var mc:MovieClip;
		public var exit:ExitObject;
		public var TestVsPlayer:Function;

		public function ExitObject()
		{
			this.name = "exit";
			this.pos = new Vector2(24, 55);
			this.trigger = new Object();
			this.trigger.pos = new Vector2(87, 39);
			this.trigger.r = Main.getInstance().tiles.xw * 0.5;
			this.isOpen = false;
			this.r = Main.getInstance().tiles.xw;
			Main.getInstance().objects.Register(this);
			this.mc = Main.getInstance().gfx.CreateSprite("debugExitMC", NinjaGraphicsSystem.LAYER_WALLS);
			this.mc.visible = false;
			this.trigger.mc = Main.getInstance().gfx.CreateSprite("debugExitTriggerMC", NinjaGraphicsSystem.LAYER_WALLS);
			this.trigger.mc.visible = false;
		}

		// TREASURE_RADIUS = 4;
		public function Destruct():void
		{
			Main.getInstance().gfx.DestroyMC(this.mc);
			this.mc = null;
			Main.getInstance().gfx.DestroyMC(this.trigger.mc);
			this.trigger.mc = null;
			this.trigger = null;
		}

		public function Init(params):void
		{
			var _local2 = params;
			if (_local2.length == 4)
			{
				this.pos.x = _local2[0];
				this.pos.y = _local2[1];
				this.trigger.pos.x = _local2[2];
				this.trigger.pos.y = _local2[3];
				this.trigger.exit = this;
				this.isOpen = false;
				this.mc.scaleX = (this.mc.scaleY = this.r * 2);
				this.mc.x = this.pos.x;
				this.mc.y = this.pos.y;
				this.mc.gotoAndStop("exit_closed");
				this.mc.visible = true;
				this.trigger.mc.scaleX = (this.trigger.mc.scaleY = this.trigger.r * 2);
				this.trigger.mc.x = this.trigger.pos.x;
				this.trigger.mc.y = this.trigger.pos.y;
				this.trigger.mc.gotoAndStop("exit_closed");
				this.trigger.mc.visible = true;
				this.trigger.TestVsPlayer = this.TestVsPlayer_Trigger;
				this.TestVsPlayer = this.TestVsPlayer_Exit;
				Main.getInstance().objects.AddToGrid(this.trigger);
				Main.getInstance().objects.Moved(this.trigger);
			}
		}

		public function UnInit():void
		{
			Main.getInstance().objects.RemoveFromGrid(this);
			Main.getInstance().objects.RemoveFromGrid(this.trigger);
		}

		public function DumpInitData():String
		{
			return "" + this.pos.x + NinjaGame.OBJPARAM_SEPERATION_CHAR + this.pos.y + NinjaGame.OBJPARAM_SEPERATION_CHAR + this.trigger.pos.x + NinjaGame.OBJPARAM_SEPERATION_CHAR + this.trigger.pos.y;
		}

		public function IdleAfterDeath():void
		{
			Main.getInstance().objects.RemoveFromGrid(this);
			Main.getInstance().objects.RemoveFromGrid(this.trigger);
		}

		public function TestVsPlayer_Exit(guy):void
		{
			if (this.isOpen)
			{
				var p = guy.pos;
				var _local3 = this.pos.x - guy.pos.x;
				var _local2 = this.pos.y - guy.pos.y;
				if (Math.sqrt((_local3 * _local3) + (_local2 * _local2)) < (this.r + guy.r))
				{
					this.PlayerHitExit();
				}
			}
		}

		public function TestVsPlayer_Trigger(guy):void
		{
			if (!this.exit.isOpen)
			{
				var p = guy.pos;
				var _local3 = this.pos.x - guy.pos.x;
				var _local2 = this.pos.y - guy.pos.y;
				if (Math.sqrt((_local3 * _local3) + (_local2 * _local2)) < (this.r + guy.r))
				{
					this.exit.PlayerHitTrigger();
				}
			}
		}

		public function PlayerHitExit():void
		{
			PlayerObject.player.Celebrate();
			Main.getInstance().App_LevelPassedEvent();
		}

		public function PlayerHitTrigger():void
		{
			this.mc.gotoAndPlay("exit_opening");
			this.isOpen = true;
			this.trigger.mc.gotoAndStop("exit_open");
			Main.getInstance().objects.RemoveFromGrid(this.trigger);
			Main.getInstance().objects.AddToGrid(this);
			Main.getInstance().objects.Moved(this);
		}
	}
}